#include "detectortest.h"

#include "testutils.h"

#define private public
#include <newcode/detector/DetectorGC.h>
#undef private

#ifdef DEBUG
#include <QDebug>
#endif

using namespace newcode;
using namespace newcode::detector;

void DetectorTest::detectionTestCase()
{
    QFETCH(QString, fileName);
    QFETCH(int, dimension);
    QString filename = TEST_DATA_BASE + fileName;
    QImage image(filename);

    if (!image.isNull())
    {
        detector::Detector detector(qImageToByteMatrix(image));
        Ref<GrayscaleDetectorResult> result = detector.detect();

        int dim = result->getBytes()->getX();

#       ifdef DEBUG
        vector<Ref<ResultPoint> > points = result->getPoints();
        qDebug() << printPoint(points[0]) <<  printPoint(points[1])
            << printPoint(points[2]) <<  printPoint(points[3]) << "dim:" << dim;
#       endif

        QCOMPARE(dim, dimension);
    }
    else
    {
        QFAIL("Image file not found");
    }
}

void DetectorTest::detectionTestCase_data()
{
    QTest::addColumn<QString>("fileName");
    QTest::addColumn<int>("dimension");
    QTest::newRow("test_2l.png") << "test_2l.png" << 14;
    QTest::newRow("test_4l.png") << "test_4l.png" << 14;
    QTest::newRow("test_8l.png") << "test_8l.png" << 14;
    QTest::newRow("test_16l.png") << "test_3l.png" << 14;
    QTest::newRow("long_2l.png") << "long_2l.png" << 62;
    QTest::newRow("long_4l.png") << "long_4l.png" << 50;
    QTest::newRow("long_8l.png") << "long_8l.png" << 42;
    QTest::newRow("long_3l.png") << "long_3l.png" << 54;
    QTest::newRow("transformed.png") << "transformed.png" << 14;
}


 QString DetectorTest::printPoint(Ref<ResultPoint> point)
 {
     QString st("");
     return st.sprintf("(%f, %f)", point->getX(), point->getY());
 }
